<?php
	include_once('misc.php');
	session_start();
	if(isset($_SESSION['cart'])) {
		$login = $_SESSION['login'];
		$db = connectdb();
		$query = "SELECT id FROM users WHERE login = '$login'";
		$res = mysql_query($query);
		if(mysql_num_rows($res) > 0) {
			$row = mysql_fetch_array($res);
			$user_id = $row['id'];
			
			$query = "INSERT INTO orders(user_id) VALUES($user_id)";
			mysql_query($query);
			$order_id = mysql_insert_id($db);
			
			foreach($_SESSION['cart'] as $book) {
				//list($book_id, $quantity) = $book;
				$book_id = $book[0];
				$quantity = $book[1];
				$query = "INSERT INTO orders_books 
							VALUES($order_id, $book_id, $quantity)";
				mysql_query($query);
			}
			unset($_SESSION['cart']);
		}
	}
?>
<!DOCTYPE html> <!-- HTML 5.0 -->
<html>
	<head>
		<meta http-equiv="Content-Type" 
					content="text/html; charset=utf-8"/>
		<title>Книжная лавка</title>
		<link rel="stylesheet" type="text/css" href="style.css"/>
	</head>
	<body>
		<div class="container">
		<!-- Header -->
		<div class="header">
			<img class="logo" width="200" height="200" alt="logo" 
						src="img/logo.png"/>
			<ul>
				<li><a href="cart.php">Корзина</a></li>
				<?php if($_SESSION['login']) { ?>
				<li><a href="logout.php">Выйти</a></li>
				<? } else { ?>
				<li><a href="reg.php">Регистрация</a></li>
				<form action="login.php" method="POST">
					<input type="textfield" name="login"/>
					<input type="password" name="pass"/>
					<input type="submit" value="Войти"/>
				</form>
				<? } ?>
			</ul>
			<div class="clear"></div>
		</div>
		<div class="main">
			<? if(isset($order_id)) { ?>
			<h1> Ваш заказ успешно обработан. Номер заказ <?=$order_id?></h1>
			  <? } else {?>
			  <h1> Заказ не обработан</h1>
			  <? } ?>
			<div class="center">
				<a href="index.php">На главную</a>
			</div>
		</div>
		<!-- footer -->
		<div class="footer">
			CC-BY PM 2012
		</div>
		</div>
	</body>
</html>
	
	
		